package com.itheima.bigevent.controller;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.itheima.bigevent.pojo.Category;
import com.itheima.bigevent.pojo.Result;
import com.itheima.bigevent.service.CategoryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: CategoryController
 * Package: com.itheima.bigevent.controller
 * Description:分类
 *
 * @Author: Zhang Peiyuan
 * @Create: 2024-08-20-15:59
 * @Version: v1.0
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 管理员接口, 添加新的文章分类
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 管理员接口, 查询当前用户自己创建的分类
     * @return
     */
    @GetMapping
    public Result<List<Category>> list() {
        return Result.success(categoryService.queryList());
    }

    /**
     * 用户接口, 用于查询所有分类
     * @return
     */
    @GetMapping("/list/all")
    public Result<List<Category>> listAll() {
        return Result.success(categoryService.queryListAll());
    }

    @GetMapping("detail/{id}")
    // @JsonView(Views.Public.class)
    public Result<Category> detail(@PathVariable("id") Integer id) {

        return Result.success(categoryService.queryById(id));
    }

    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteList(@RequestParam("id") Integer id) {
        categoryService.deleteById(id);
        return Result.success();
    }
}
